var path = getAdminPath();
var isTitle = 0, isFromImg = 0, isChangeImg = 0; //是否为标题点击 , 图片点击 ,修改小图
var oprationIndex = 0; //操作次数
var windowScrollTop = 0;
var editData = {};
// var articleVersionNum;
var _articleInfo;
var editTipsTaged = 0; //编辑提示手势框 是否已点击
$(function () {
    // articleVersionNum = localStorage.articleVersionNum;
    // if (articleVersionNum == 1) {  // 判断新老文章
    //     getOldArticleDetail();
    // } else {
    // }
    initAppkey()
    //正文编辑
    $(".edit-context").on("click", function (e) {
        e = window.event || e; // 兼容IE7
        var etarget = e.srcElement || e.target;
        isTitle = 0;
        var $obj = $(etarget);
        var $editBox = $("#editBox");
        if ($editBox.length > 0) {
            $editBox.prev().show();
            if (isChangeImg) {
                $editBox.prev().removeClass("opacit0");
                $editBox.parent().css({ "height": "auto" });
                $editBox.prev().css("margin-bottom", "initial");
                isChangeImg = 0;
            }
        }
        $("#editBox").remove();
        var editBoxNode;
        if ($($obj).is("#img,#img *,img")) {
            isFromImg = 1;
            // if (articleVersionNum == 1) {
            //     editBoxNode = '<div class="edit-box " id="editBox" onclick="editboxTapTwice(event)">' +
            //         '<div class="editbox-body">' +
            //         $obj.parent().prop("outerHTML") +
            //         '</div><div class="editbox-footer flex">' +
            //         '<div class="editbox-footer-item flex-1" onclick="deletSection(event)">删除图片</div>' +
            //         '<div class="editbox-footer-item flex-1" onclick="tap2exitSection(event)">插入文字</div>' +
            //         '<div class="editbox-footer-item flex-1" onclick="insertImage(event)">插入图片</div>' +
            //         '</div></div>';

            //     $obj.parent().hide();
            //     $obj.parent().after(editBoxNode);
            //     if ($obj.css("float")) {
            //         $("#editBox").find("img").css("float", "none");
            //     }
            // } else {
            //     editBoxNode = '<div class="edit-box " id="editBox" onclick="editboxTapTwice(event)">' +
            //         '<div class="editbox-body">' +
            //         $obj.prop("outerHTML") +
            //         '</div><div class="editbox-footer flex">' +
            //         '<div class="editbox-footer-item flex-1" onclick="deletSection(event)">删除图片</div>' +
            //         '<div class="editbox-footer-item flex-1" onclick="tap2exitSection(event)">插入文字</div>' +
            //         '<div class="editbox-footer-item flex-1" onclick="insertImage(event)">插入图片</div>' +
            //         '</div></div>';

            //     if ($obj.parent()[0].tagName == "SPAN") {
            //         $obj.parent().css("display", "block");
            //     }
            //     $obj.hide();
            //     $obj.after(editBoxNode);
            //     if ($obj.css("float")) {
            //         $("#editBox").find("img").css("float", "none");
            //     }
            //     // 当图片小于 内容框 宽度时 使用这个
            //     console.log($obj.parent().width(), $("#editContentList").width() * 0.9)
            //     if ($obj.parent().width() < $("#editContentList").width() * 0.9) {
            //         $obj.show();
            //         isChangeImg = 1;
            //         $obj.addClass("opacit0");
            //         if (!($obj.parent().css("position")) || ($obj.parent().css("position")) == "static") {
            //             $obj.parent().css("position", "relative");
            //         }
            //         if (!($obj.parent().css("overflow")) || ($obj.parent().css("overflow")) == "hidden") {
            //             $obj.parent().css("overflow", "initial");
            //         }
            //         console.log($obj.offset().top, $obj.parent().offset().top);
            //         $("#editBox").css({
            //             "width": "27.36rem",
            //             "height": "auto",
            //             "position": "absolute",
            //             "left": (($(window).width() / 2) - $obj.parent().offset().left - $("#editBox").width() / 2 - 10) + "px",
            //             "top": ($obj.offset().top - $obj.parent().offset().top) + "px",
            //             "margin": "auto",
            //             "background": "#f1dddc ",
            //             "z-index": "9",
            //         });
            //         console.log($("#editBox").height() + 20 - $obj.height())
            //         $obj.css("margin-bottom", ($("#editBox").height() + 20 - $obj.height()) + "px")
            //     }
            // }
            editBoxNode = '<div class="edit-box " id="editBox" onclick="editboxTapTwice(event)">' +
                '<div class="editbox-body">' +
                $obj.prop("outerHTML") +
                '</div><div class="editbox-footer flex">' +
                '<div class="editbox-footer-item flex-1" onclick="deletSection(event)">删除图片</div>' +
                '<div class="editbox-footer-item flex-1" onclick="tap2exitSection(event)">插入文字</div>' +
                '<div class="editbox-footer-item flex-1" onclick="insertImage(event)">插入图片</div>' +
                '</div></div>';

            if ($obj.parent()[0].tagName == "SPAN") {
                $obj.parent().css("display", "block");
            }
            $obj.hide();
            $obj.after(editBoxNode);
            if ($obj.css("float")) {
                $("#editBox").find("img").css("float", "none");
            }
            // 当图片小于 内容框 宽度时 使用这个
            console.log($obj.parent().width(), $("#editContentList").width() * 0.9)
            if ($obj.parent().width() < $("#editContentList").width() * 0.9) {
                $obj.show();
                isChangeImg = 1;
                $obj.addClass("opacit0");
                if (!($obj.parent().css("position")) || ($obj.parent().css("position")) == "static") {
                    $obj.parent().css("position", "relative");
                }
                if (!($obj.parent().css("overflow")) || ($obj.parent().css("overflow")) == "hidden") {
                    $obj.parent().css("overflow", "initial");
                }
                console.log($obj.offset().top, $obj.parent().offset().top);
                $("#editBox").css({
                    "width": "27.36rem",
                    "height": "auto",
                    "position": "absolute",
                    "left": (($(window).width() / 2) - $obj.parent().offset().left - $("#editBox").width() / 2 - 10) + "px",
                    "top": ($obj.offset().top - $obj.parent().offset().top) + "px",
                    "margin": "auto",
                    "background": "#f1dddc ",
                    "z-index": "9",
                });
                console.log($("#editBox").height() + 20 - $obj.height())
                $obj.css("margin-bottom", ($("#editBox").height() + 20 - $obj.height()) + "px")
            }
        } else if ($($obj).hasClass("opacity0div")) {
            editBoxNode = '<div class="edit-box " id="editBox" onclick="editboxTapTwice(event)">' +
                '<div class="editbox-body">' +
                $obj.parent().prop("outerHTML") +
                '</div><div class="editbox-footer flex">' +
                '<div class="editbox-footer-item flex-1"  onclick="deletSection(event)">删除</div>'
            '</div></div>';
            console.log($obj.parent().prop("outerHTML"));
            $obj.parent().after(editBoxNode);
            $obj.parent().hide();
        } else {
            isFromImg = 0;
            // var ediTCcontextWidth = $(".edit-context").width() * 0.9;
            var $obj_insert = null;
            while (!checkFontSection($obj)) {
                if ($obj.parent().length > 0) {
                    $obj = $obj.parent();
                } else {
                    break;
                }
            }
            $obj_insert = $obj;
            var marginLeft_insert = "0px", marginTop_insert = "0px", marginRight_insert = "0px", marginBottom_insert = "0px";
            if ($obj_insert.css("marginLeft")) {
                if (parseFloat($obj_insert.css("marginLeft").split("px")[0]) < 0) {
                    marginLeft_insert = (-parseFloat($obj_insert.css("marginLeft").split("px")[0])) + "px"
                } else {
                    marginLeft_insert = parseFloat($obj_insert.css("marginLeft").split("px")[0]) + "px"
                }
            }
            if ($obj_insert.css("marginTop")) {
                if (parseFloat($obj_insert.css("marginTop").split("px")[0]) < 0) {
                    console.log(-parseFloat($obj_insert.css("marginTop").split("px")[0]))
                    marginTop_insert = (-parseFloat($obj_insert.css("marginTop").split("px")[0])) + "px"
                } else {
                    marginTop_insert = parseFloat($obj_insert.css("marginTop").split("px")[0]) + "px"
                }
            }
            if ($obj_insert.css("marginRight")) {
                if (parseFloat($obj_insert.css("marginRight").split("px")[0]) < 0) {
                    console.log(-parseFloat($obj_insert.css("marginRight").split("px")[0]))
                    marginRight_insert = (-parseFloat($obj_insert.css("marginRight").split("px")[0])) + "px"
                } else {
                    marginRight_insert = parseFloat($obj_insert.css("marginRight").split("px")[0]) + "px"
                }
            }
            if ($obj_insert.css("marginBottom")) {
                if (parseFloat($obj_insert.css("marginBottom").split("px")[0]) < 0) {
                    console.log(-parseFloat($obj_insert.css("marginBottom").split("px")[0]))
                    marginBottom_insert = (-parseFloat($obj_insert.css("marginBottom").split("px")[0])) + "px"
                } else {
                    marginBottom_insert = parseFloat($obj_insert.css("marginBottom").split("px")[0]) + "px"
                }
            }
            var style_insert = "style='margin-left:" + marginLeft_insert + ";margin-top:" + marginTop_insert + ";margin-right:" + marginRight_insert + ";margin-bottom:" + marginBottom_insert + ";'"
            editBoxNode = '<div class="edit-box " id="editBox" onclick="editboxTapTwice(event)">' +
                '<div class="editbox-body" ' + style_insert + '>' +
                $obj_insert.prop("outerHTML") +
                '</div><div class="editbox-footer flex">' +
                '<div class="editbox-footer-item flex-1" onclick="deletSection(event)">删除文字</div>' +
                '<div class="editbox-footer-item flex-1" onclick="tap2exitSection(event)">修改文字</div>' +
                '<div class="editbox-footer-item flex-1" onclick="insertImage(event)">插入图片</div>' +
                '</div></div>';
            $obj_insert.after(editBoxNode);
            $obj_insert.hide();
        }
    });
    //标题编辑
    $(".edit-title").on("click", function (e) {
        isTitle = 1;
        var $editBox = $("#editBox");
        if ($editBox.length > 0) {
            if (isChangeImg) {
                $editBox.prev().removeClass("opacit0");
                $editBox.parent().css({ "height": "auto" });
                $editBox.prev().css("margin-bottom", "initial");
                isChangeImg = 0;
            }
            $editBox.prev().show();
        }
        $("#editBox").remove();
        $("#exitTitleNum").html("0");
        $("#exitTitleInput").val("");
        tap2titleInput(e);  //老文章直接弹起修改弹框
        // 遗弃
        // if( articleVersionNum == 1){  // 判断新老文章
        //     $("#exitTitleNum").html("0");
        //     $("#exitTitleInput").val(""); 
        //     tap2titleInput(e);  //老文章直接弹起修改弹框
        // }else{ 
        //     var editBoxNode = '<div class="edit-box " id="editBox">' + 
        //             '<div class="editbox-body"><p>' +
        //             $(this).html() + 
        //             '</p></div><div class="editbox-footer flex">' +
        //             '<div class="editbox-footer-item flex-1" onclick="tap2titleInput(event)">修改标题</div>' +
        //             '<div class="editbox-footer-item flex-1" onclick="tap2exitSection(event)">插入文字</div>'+
        //             '<div class="editbox-footer-item flex-1" onclick="insertImage(event)">插入图片</div>'+
        //             '</div></div>' ;
        //     $(this).hide();
        //     $(this).after(editBoxNode); 
        //     $("#exitTitleNum").html("0");
        //     $("#exitTitleInput").val(""); 
        // }                  
    });
    //标题输入框输入
    $("#exitTitleInput").on('input propertychange', function () {
        var strVal = $(this).val();
        if (strVal.length > 60) {
            showTit("标题字数不可超过60！", "warn", function () {
                $(this).val(strVal.substring(0, 60));
            });
            return false;
        }
        $("#exitTitleNum").html(strVal.length);
    })
});
// 获取appKey
function initAppkey(){ 
    var url = path + "soft/getWapConfig";
    var sendData = {};
    var callback = function (res) {
        let resData = res.data ;
        window.sessionStorage.setItem("appKey" , resData.appKey) ;
        window.sessionStorage.setItem("jmsz" , resData.jmsz) ;  
        getArticleDetail();
    };
    Public.ajaxLoadData(0, url, sendData, "POST", "JSON", callback);
}
// 判断 字体标签选中时的满足情况
function checkFontSection($that) {
    var _ediTCcontextWidth = $(".edit-context").width() * 0.9;
    if (($that.width() >= _ediTCcontextWidth || $that.parent().hasClass("edit-context") || $that.parent().hasClass("contentitem")) && ($that[0].tagName != "TBODY" && $that[0].tagName != "TH" && $that[0].tagName != "TR" && $that[0].tagName != "TD" && $that[0].tagName != "STRONG" && $that[0].tagName != "A" && $that[0].tagName != "B" && $that[0].tagName != "CANVAS" && $that[0].tagName != "EM" && $that[0].tagName != "I" && $that[0].tagName != "NAV")) {
        return true;
    } else if (($that.width() <= _ediTCcontextWidth && ($that.parent().hasClass("edit-context") || $that.parent().hasClass("contentitem"))) && ($that[0].tagName != "TBODY" && $that[0].tagName != "TH" && $that[0].tagName != "TR" && $that[0].tagName != "TD" && $that[0].tagName != "STRONG" && $that[0].tagName != "A" && $that[0].tagName != "B" && $that[0].tagName != "CANVAS" && $that[0].tagName != "EM" && $that[0].tagName != "I" && $that[0].tagName != "NAV")) {
        return true;
    } else {
        return false
    }
}
// 编辑框重新点击 取消选中
function editboxTapTwice(event) {
    event.stopPropagation();
    event.preventDefault();
    var $editBox = $("#editBox");
    if ($editBox.length > 0) {
        $editBox.prev().show();
        if (isChangeImg) {
            $editBox.prev().removeClass("opacit0");
            $editBox.parent().css({ "height": "auto" });
            $editBox.prev().css("margin-bottom", "initial");
            isChangeImg = 0;
        }
    }
    $("#editBox").remove();
}

// //获取文章详情 老文章  （articleVersionNum: 1）
// function getOldArticleDetail() {
//     var query = getCNParam();
//     var url = path + "getPreEditArticleInfo";
//     var sendData = {
//         articleId: query.articleId
//     };
//     var callback = function (res) {
//         var resData = res.data;
//         $(".edit-title").html(resData.articleTitle);
//         _articleInfo = resData.articleInfo;
//         (resData.articleInfo).forEach(function (item, index) {
//             if ((item.textType == 1) && (item.textContent) && (item.textContent).indexOf("iframe") > -1) {
//                 item.isIframe = 1;
//             } else {
//                 item.isIframe = 0;
//             }
//         });
//         // setTimeout(function () {
//         $("#editContentList").html($("#editContentListTmpl").tmpl(resData));  //老文章内容 

//         $("#editContentList").trigger("create");
//         setVideoSrc();
//         // audioSetup();
//         resetContentStyle();
//         // }, 300);
//         $(".editTips").show();
//         editData.articleId = resData.articleId;
//         editData.saveType = 2;
//         editData.articleImgUrl = resData.articleImgUrl;
//         if (resData.articleTypeId) {
//             editData.articleTypeId = parseInt(resData.articleTypeId);
//         }
//         if (resData.articleBrandId) {
//             editData.articleBrandId = resData.articleBrandId;
//         }
//         setTimeout(function () {
//             $(".editTips").hide();
//         }, 2000);

//     };
//     Public.ajaxLoadData(0, url, sendData, "POST", "JSON", callback);
// }
//获取文章详情 新文章  （articleVersionNum= 2）
function getArticleDetail() {
    var query = getCNParam();
    var url = path + "article/getArticleDetailInfo";
    var sendData = {
        articleId: query.articleId,
        shareUniqueId: query.shareUniqueId,
        uniqueId: window.localStorage.uniqueId,
    };
    var callback = function (res) {
        var resData = res.data.articleDetailInfo.articleInfo;
        $(".edit-title").html(resData.articleTitle);
        $(".edit-context").html(resData.articleContent);
        $(".edit-title").trigger("create");
        $(".edit-context").trigger("create");
        $(".editTips").show();
        editData.articleId = resData.id;
        editData.articleSource = 3;
        editData.articleThumbnailsUrl = resData.articleThumbnailsUrl;
        editData.articleOverview = resData.articleOverview;
        // setVideoSrc();
        // audioSetup();
        resetContentStyle();
        setTimeout(function () {
            $(".editTips").hide();
            if (!editTipsTaged) {
                // $("#editContentList").children().eq(0)[0].click(); 
                findFirstChild();
            }
        }, 2000);
    };
    Public.ajaxLoadData(0, url, sendData, "POST", "JSON", callback);
}
//hide tips
function hideEditTips(event) {
    event.stopPropagation();
    event.preventDefault();
    $(".editTips").hide();
    // $("#editContentList").children().eq(0)[0].click();
    findFirstChild();
    editTipsTaged = 1;
}
// 寻找内容 （ #editContentList ）第一个可编辑子元素
function findFirstChild() {
    var $exidChildren1 = $("#editContentList").children().eq(0);
    if ($exidChildren1.children().length > 0) {
        while ($exidChildren1.children().length > 0) {
            $exidChildren1 = $exidChildren1.children().eq(0);
        }
        $exidChildren1[0].click();
    }

}
// exitbox 重复点击消失
function tapAgainExitbox(event) {
    event.stopPropagation();
    event.preventDefault();
    $("#editBox").hide();
    return false;
}
// 正文 删除文字 | 段落 | 图片
function deletSection(event) {
    event.stopPropagation();
    event.preventDefault();
    var $editBox = $("#editBox");
    oprationIndex += 1;
    $(".edit-footer-cancel").html("撤销");
    if ($editBox.length > 0) {
        if (isChangeImg) {
            $editBox.prev().removeClass("opacit0");
            $editBox.parent().css({ "height": "auto" });
            $editBox.prev().css("margin-bottom", "initial");
            isChangeImg = 0;
        }
        $editBox.prev().hide();
        var prevNode = $editBox.prev().prop("outerHTML");
        $editBox.before(prevNode);
        $editBox.prev().addClass(" opration" + oprationIndex);
    }
    $("#editBox").remove();
}
// 正文 | 标题  插入图片 
function insertImage(event) {
    event.stopPropagation();
    event.preventDefault();
    var $editBox = $("#editBox");
    oprationIndex += 1;
    $(".edit-footer-cancel").html("撤销");
    if ($editBox.length > 0 && isTitle) {
        $editBox.prev().hide();
        // $(".edit-context").prepend("<p class='opration"+ oprationIndex +"' style='margin:20px;text-align:center;border:1px solid blue'>插入图片</p>");
        //插入图片  使用wx.chooseImage
        generateJsConfig( "pre", oprationIndex);
    } else if ($editBox.length > 0) {
        $editBox.prev().hide();
        // $editBox.prev().after("<p class='opration"+ oprationIndex +"' style='margin:20px;text-align:center;border:1px solid blue'>插入图片</p>")
        generateJsConfig("after", oprationIndex);
    }

}
//正文 修改文字 弹窗
function tap2exitSection(event) {
    event.stopPropagation();
    event.preventDefault();
    windowScrollTop = $(window).scrollTop();
    var $editBox = $("#editBox");
    if ($editBox.length > 0 && isTitle) {
        console.log($(".edit-title"));
        var textBr2Enter_title = ($(".edit-title").html());
        $(".exitSection-input").html(textBr2Enter_title);
    } else if ($editBox.length > 0 && isFromImg) {
        $(".exitSection-input").html("");
    } else if ($editBox.length > 0) {
        var $contentitem = $editBox.find(".editbox-body").find(".contentitem"), textBr2Enter_body;
        if ($contentitem.length > 0) {
            textBr2Enter_body = ($contentitem.html());
        } else {
            textBr2Enter_body = ($editBox.find(".editbox-body").html());
        }
        $(".exitSection-input").html(textBr2Enter_body);
    }
    $(".editArticle").hide();
    $(".exitsectionwrap").show();
}
// 修改文字 弹窗 取消
function tap2cancelContent(event) {
    event.stopPropagation();
    event.preventDefault();
    var $editBox = $("#editBox");
    if ($editBox.length > 0) {
        if (isChangeImg) {
            $editBox.prev().removeClass("opacit0");
            $editBox.parent().css({ "height": "auto" });
            $editBox.prev().css("margin-bottom", "initial");
            isChangeImg = 0;
        }
        $editBox.prev().show();
    }
    $("#editBox").remove();
    $(".editArticle").show();
    $(window).scrollTop(windowScrollTop);
    $(".exitsectionwrap").hide();
}
// 修改文字 弹窗 删除
function tap2deleteInput(event) {
    event.stopPropagation();
    event.preventDefault();
    $(".exitSection-input").html("");
}
// 修改文字 弹窗 保存
function tap2saveInput(event) {
    event.stopPropagation();
    event.preventDefault();
    var inputValue = $(".exitSection-input").text();
    var changeVal2Html = $(".exitSection-input").html();
    if (inputValue.length <= 0) { //内容删除为空时不必调用敏感接口
        saveChangeHtml(changeVal2Html);
        return false;
    }
    // 校验编辑内容是否含有敏感词 
    var url = path + "article/scanTextContent";
    var sendData = {
        textContent: inputValue.replace(/<\/?.+?>/g, "").replace(/s/g, "").replace(/\r\n/g, ' ').replace(/\n/g, ' ')
    }
    var callback = function (res) {
        var resData = res.data;
        if (resData.scanStatus == 0) {
            showTit(resData.scanMsg, "warn");
            return false;
        } else {
            saveChangeHtml(changeVal2Html);
        }

    };
    Public.ajaxLoadData(0, url, sendData, "POST", "json", callback);
}
// 编辑内容框 保存内容
function saveChangeHtml(changeVal2Html) {
    var $editBox = $("#editBox");
    oprationIndex += 1;
    $(".edit-footer-cancel").html("撤销");
    if ($editBox.length > 0 && isTitle) {
        if (isChangeImg) {
            $editBox.prev().removeClass("opacit0");
            $editBox.parent().css({ "height": "auto" });
            $editBox.prev().css("margin-bottom", "initial");
            isChangeImg = 0;
        }
        $editBox.prev().show();
        $(".edit-context").prepend("<p class='opration" + oprationIndex + " hasNotPrevNode insertFontStyle'>" + changeVal2Html + "</p>");
        $editBox.prev().show();
    } else if ($editBox.length > 0 && isFromImg) {
        if (isChangeImg) {
            $editBox.prev().removeClass("opacit0");
            $editBox.parent().css({ "height": "auto" });
            $editBox.prev().css("margin-bottom", "initial");
            isChangeImg = 0;
        }
        $editBox.prev().show();
        // if (articleVersionNum == 1) {
        //     $editBox.prev().after("<p class='contentitem opration" + oprationIndex + " hasNotPrevNode insertFontStyle' data-type='1'  data-contxt='" + changeVal2Html + "'>" + changeVal2Html + "</p>");
        // } else {
        //     $editBox.prev().after("<p class='opration" + oprationIndex + " hasNotPrevNode insertFontStyle' data-type='1'  data-contxt='" + changeVal2Html + "'>" + changeVal2Html + "</p>");
        // }
        $editBox.prev().after("<p class='opration" + oprationIndex + " hasNotPrevNode insertFontStyle' data-type='1'  data-contxt='" + changeVal2Html + "'>" + changeVal2Html + "</p>");
    } else if ($editBox.length > 0) {
        var prevNode = $editBox.prev().prop("outerHTML");
        $editBox.prev().hide();
        $editBox.before(prevNode);
        $editBox.prev().html(changeVal2Html);
        $editBox.prev().attr("data-type", "1");
        $editBox.prev().attr("data-contxt", changeVal2Html);
        // if (articleVersionNum == 1) {
        //     $editBox.prev().addClass("contentitem opration" + oprationIndex);
        // } else {
        //     $editBox.prev().addClass("opration" + oprationIndex);
        // }
        $editBox.prev().addClass("opration" + oprationIndex);
        if (isChangeImg) {
            $editBox.prev().removeClass("opacit0");
            $editBox.parent().css({ "height": "auto" });
            $editBox.prev().css("margin-bottom", "initial");
            isChangeImg = 0;
        }
        $editBox.prev().show();
    }
    $("#editBox").remove();
    $(".editArticle").show();
    $(window).scrollTop(windowScrollTop);
    $(".exitsectionwrap").hide();
    
    console.log(oprationIndex)
}
// tap 2 标题修改 弹窗
function tap2titleInput(event) {
    event.stopPropagation();
    event.preventDefault();
    splitBodyHeight(); // 截取body高度为窗口大小
    var titleVal = $(".edit-title").html();
    $("#exitTitleNum").html(titleVal.length);
    var textBr2Enter_titleVal = textBr2Enter(titleVal);
    $("#exitTitleInput").val(textBr2Enter_titleVal);
    $(".settingtips").hide();
    $(".exitTitleWrap").show();
}
//tap 隐藏 标题修改弹窗
function tapHideTitleInput(event) {
    event.stopPropagation();
    event.preventDefault();
    returnBodyHeight(); // 恢复body高度 
    var $editBox = $("#editBox");
    if ($editBox.length > 0) {
        if (isChangeImg) {
            $editBox.prev().removeClass("opacit0");
            $editBox.parent().css({ "height": "auto" });
            $editBox.prev().css("margin-bottom", "initial");
            isChangeImg = 0;
        }
        $editBox.prev().show();
    }
    $("#exitTitleNum").html("0");
    $("#exitTitleInput").val("");
    $("#editBox").remove();
    $(".settingtips").hide();
}
//tap 2 保存 标题输入
function tap2SaveTitle(event) {
    event.stopPropagation();
    event.preventDefault();
    returnBodyHeight(); // 恢复body高度 
    var _titleVal = ($("#exitTitleInput").val()).replace(/\r\n/g, '').replace(/\n/g, '').replace(/\s/g, '');
    if (_titleVal.length <= 0) {
        showTit("文章标题不可为空！", "warn");
        return false;
    } 
    // 校验编辑内容是否含有敏感词 
    var url = path + "article/scanTextContent";
    var sendData = {
        textContent: _titleVal
    }
    var callback = function (res) {
        var resData = res.data;
        if (resData.scanStatus == 0) {
            showTit(resData.scanMsg, "warn");
            return false;
        } else {
            var $editBox = $("#editBox");
            if ($editBox.length > 0) {
                if (isChangeImg) {
                    $editBox.prev().removeClass("opacit0");
                    $editBox.parent().css({ "height": "auto" });
                    $editBox.prev().css("margin-bottom", "initial");
                    isChangeImg = 0;
                }
                $editBox.prev().show();
            }
            $(".edit-title").html(textEnter2Br($("#exitTitleInput").val()));
            $(".settingtips").hide();
            $("#editBox").remove();
        }

    };
    Public.ajaxLoadData(0, url, sendData, "POST", "json", callback);

}
//撤销 编辑操作
function exitCancel(event) {
    event.stopPropagation();
    event.preventDefault();
    if (oprationIndex <= 0) {
        $(".edit-footer-cancel").html("取消");
        // localStorage.articleVersionNum = null;
        window.history.go(-1);
        return false;
    }
    console.log(oprationIndex)
    var $node = $("body").find(".opration" + oprationIndex);
    if ($node.hasClass("hasNotPrevNode")) {
        $node.remove();
        $(window).scrollTop(0)
    } else {
        var $editBox = $("#editBox");
        if ($editBox.length > 0) {
            $editBox.prev().show();
            if (isChangeImg) {
                $editBox.prev().removeClass("opacit0");
                $editBox.parent().css({ "height": "auto" });
                $editBox.prev().css("margin-bottom", "initial");
                isChangeImg = 0;
            }
            editBox.remove();
        }
        $node.prev().show();
        console.log($node.prev())
        if ($node.prev().offset().top < $(window).scrollTop()) {
            $(window).scrollTop($node.prev().offset().top - 50);
        }
        $node.remove();
    }
    oprationIndex -= 1;
    if (oprationIndex <= 0) {
        $(".edit-footer-cancel").html("取消");
        return false;
    }

}
//保存 文章编辑
function exitSave() {
    var $editBox = $("#editBox");
    if ($editBox.length > 0) {
        if (isChangeImg) {
            $editBox.prev().removeClass("opacit0");
            $editBox.parent().css({ "height": "auto" });
            $editBox.prev().css("margin-bottom", "initial");
            isChangeImg = 0;
        }
        $editBox.prev().show();
    }
    $("#editBox").remove();
    var articleTitle = $(".edit-title").html();
    if (checkStringIsNull(articleTitle)) {
        showTit("标题不可为空！", "warn");
        return false;
    }
    $(".opacity0div").remove();
    editData.articleTitle = articleTitle;  
    editData.articleContent = $(".edit-context").html();
    editData.uniqueId = window.localStorage.uniqueId;
    // editData.articleVersionNum = articleVersionNum == 1 ? 1 : 2;
    // console.log(editData);
    // console.log(editData.articleContent);
    // console.log( $(".edit-context").html() );
    var url = path + "article/reviseArticleInfo";
    var callback = function (res) {
        var resData = res.data;
        var query = getCNParam();
        showTit("保存成功！", "warn", function () {
            // localStorage.articleVersionNum = null;  
            let turnUrl = `${window.location.origin}#/adIndex/articleDetail?articleId=${resData.articleId}&articleTitle=${editData.articleTitle}&shareUniqueId=${query.shareUniqueId}`;
            window.location.replace(turnUrl);
        })
    };
    Public.ajaxLoadData(0, url, editData, "POST", "json", callback);
}
// 重置 文章内容样式
function resetContentStyle() {
    // 爬文 标签width强制平铺 
    var $tag = $("#editContentList *");
    $tag.each(function () {
        $(this).css("max-width", "100%")
        if ($(this).css("background-image").indexOf("https://mmbiz.qpic.cn") > -1) {
            $(this).css("background-image", "");
        }
        if ($(this).css("background").indexOf("https://mmbiz.qpic.cn") > -1) {
            $(this).css("background", "");
        }
        if ($(this).width() > $(".edit-context").width()) {
            // console.log($(this))
            $(this).css({
                "width": ($(".edit-context").width() + "px")
            })
        }
        // if($(this).css("overflow").indexOf("hidden")> -1){
        //     $(this).addClass("hasHidden");
        //     $(this).css("overflow","auto")
        // }
        if ($(this).is("#iframe,#iframe *,iframe")) {
            if (($(this).parent().css("display") != "none")) {
                if (!($(this).prev().hasClass("vedioIframe"))) {
                    $(this).parent().css({
                        "position": "relative",
                        "display": "block"
                    });
                    $(this).before('<div class="opacity0div vedioIframe"></div>')
                    if ($(this).parent().is("#p,#p *,p")) {
                        $(this).parent().html("<div style='position:relative;'> " + $(this).parent().html() + "</div>")
                    } else if ($(this).parent().is("#span,#span *,span")) {
                        $(this).parent().html("<div style='position:relative;'> " + $(this).parent().html() + "</div>")
                    }
                }

            }
        }
        var tagClassName = $(this).attr("class");
        if (tagClassName && tagClassName.indexOf("opration") > -1) {
            var tagOprationIndex = tagClassName.split("opration")[1];
            $(this).removeClass("opration" + tagOprationIndex);
        }
    });
    var $tagImg = $("#editContentList img");
    $tagImg.each(function () {
        if ($(this).attr("data-src")) {
            $(this).attr("src", $(this).attr("data-src"));
        }
        var _this = this;
        $(_this).load(function () {
            if ($(_this).width() >= ($(".edit-context").width() * 0.8)) {
                console.log($(_this).css("display"));
                if ($(_this).css("display") !== "none") {
                    $(_this).css({
                        "width": $(".edit-context").width() + "px",
                        "height": "auto",
                        "display": "block"
                    });
                    $(_this).css("max-width", "100%")
                }
                $(_this).attr("width", $(".edit-context").width() + "");
            } else if ($(_this).width() >= ($(".edit-context").width() / 3)) {

            }
        });
    });
    var $audioMP = $(".audio-sectionmp");
    var $audioQQ = $(".audio-section");
    if ($audioMP.length > 0 || $audioQQ.length > 0) {
        $(".audio-section").before('<div class="opacity0div vedioIframe"></div>')
    }
    var $mpvoice = $("mpvoice");
    if ($mpvoice.length > 0) {
        $mpvoice.each(function (index) {
            var voiceTitle;
            if (!checkStringIsNull($(this).attr("name"))) {
                voiceTitle = $(this).attr("name");
            } else if (!checkStringIsNull($(this).attr("data-name"))) {
                voiceTitle = $(this).attr("data-name");
            } else {
                voiceTitle = ("未知歌曲");
            }
            var voiceFileId;
            if (!checkStringIsNull($(this).attr("voice_encode_fileid"))) {
                voiceFileId = $(this).attr("voice_encode_fileid")
            } else if (!checkStringIsNull($(this).attr("data-voice_encode_fileid"))) {
                voiceFileId = $(this).attr("data-voice_encode_fileid")
            }
            var audioHtml = '<div class="contentitem" > <div class="opacity0div vedioIframe"></div>' +
                '<div id="js-audio-section' + index + '" class="audio-section">' +
                '<div id="js-audio-button' + index + '" class="audio-button">' +
                '<div id="js-play' + index + '" class="play" style="width:40px;height:40px;">' +
                '<span class="iconfont icon-pcduanbizhixiazaicishutubiao"></span>' +
                '</div>' +
                '<div id="js-pause' + index + '" class="pause" style="width:40px;height:40px;">' +
                '<span class="iconfont icon-yinlezanting"></span>' +
                '</div>' +
                '</div>' +
                '<span id="js-audio-name' + index + '" class="audio-name">' + voiceTitle + '</span>' +
                '<span id="js-audio-duration' + index + '" class="audio-duration"></span>' +
                '<audio id="js-audio' + index + '" data-source="https://res.wx.qq.com/voice/getvoice?mediaid=' + voiceFileId + '"  data-name="' + voiceTitle + '">' +
                '<source src="https://res.wx.qq.com/voice/getvoice?mediaid=' + voiceFileId + '" type="audio/mpeg">' +
                '<source src="https://res.wx.qq.com/voice/getvoice?mediaid=' + voiceFileId + '" type="auido/mp3">' +
                '</audio>' +
                '</div></div>';
            $(this).parent().html(audioHtml);
        });
    }

    var $qqmusic = $("qqmusic");
    var qqmusicArry = [];
    var midurlinfo = [];
    if ($qqmusic.length > 0) {
        $qqmusic.each(function (item) {
            qqmusicArry.push($(this).attr("mid"));
        });
        var data = {
            "req": {
                "module": "CDN.SrfCdnDispatchServer",
                "method": "GetCdnDispatch",
                "param": {
                    "guid": "4680094450",
                    "calltype": 0,
                    "userip": ""
                }
            },
            "req_0": {
                "module": "vkey.GetVkeyServer",
                "method": "CgiGetVkey",
                "param": {
                    "guid": "4680094450",
                    "songmid": qqmusicArry,
                    "songtype": [0],
                    "uin": "",
                    "loginflag": 1,
                    "platform": "20"
                }
            },
            "comm": {
                "uin": '',
                "format": "json",
                "ct": 24,
                "cv": 0
            }
        }
        let urlqq = 'https://u.y.qq.com/cgi-bin/musicu.fcg?-=getplaysongvkey5750188977044803&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0&data=' + JSON.stringify(data);
        $.ajax({
            url: urlqq,
            type: "get",
            dataType: 'jsonp',
            scriptCharset: 'GBK',//解决中文乱码
            success: function (data) {
                //最新音乐数据
                data.req_0.data.midurlinfo.forEach((item, index) => {
                    midurlinfo.push(item.purl)
                })
                $qqmusic.each(function (index) {
                    var music_name = checkStringIsNull($(this).attr('music_name')) ? '其他歌曲' : $(this).attr('music_name');
                    var audioHtml_qq = '<div class="contentitem" > <div class="opacity0div vedioIframe"></div>' +
                        '<div id="js-audio-section' + index + '" class="audio-section">' +
                        '<div id="js-audio-button' + index + '" class="audio-button">' +
                        '<div id="js-play' + index + '" class="play" style="width:40px;height:40px;">' +
                        '<span class="iconfont icon-pcduanbizhixiazaicishutubiao"></span>' +
                        '</div>' +
                        '<div id="js-pause' + index + '" class="pause" style="width:40px;height:40px;">' +
                        '<span class="iconfont icon-yinlezanting"></span>' +
                        '</div>' +
                        '</div>' +
                        '<span id="js-audio-name' + index + '" class="audio-name">' + music_name + '</span>' +
                        '<span id="js-audio-duration' + index + '" class="audio-duration"></span>' +
                        '<audio id="js-audio' + index + '" data-source="http://dl.stream.qqmusic.qq.com/' + midurlinfo[index] + '"  data-name="' + music_name + '">' +
                        '<source src="http://dl.stream.qqmusic.qq.com/' + midurlinfo[index] + '" type="audio/mpeg">' +
                        '<source src="http://dl.stream.qqmusic.qq.com/' + midurlinfo[index] + '" type="auido/mp3">' +
                        '</audio>' +
                        '</div></div>';
                    $(this).parent().html(audioHtml_qq);
                });
            },
            error: function (e) {
                console.log(e);
            }
        });
    }
}